package com.bawei.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueueConfig {
    // 交换机
    private static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    // 队列
    private static final String DELAYED_QUEUE_NAME = "delayed.queue";
    // routingKey
    private static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    // 声明交换机
    @Bean
    public CustomExchange customExchange() {

        Map<String, Object> arguments = new HashMap<>();
        /**
         * 参数说明：
         * 1.交换机的名称
         * 2.交换机的类型
         * 3.是否需要持久化
         * 4.是否自动删除
         * 5.其它参数
         */
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message", true, false, arguments);
    }

    // 声明队列
    @Bean
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }

    // 绑定
    @Bean
    public Binding delayedQueueBindingDelayedExchange( Queue delayedQueue,CustomExchange delayedExchange
    ) {
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}
